﻿using System;
using System.Windows;
using System.Windows.Media;
using HandyControl.Tools;
using OpenFileTransfer.Wpf.Models;
using Prism.Commands;
using Prism.Mvvm;

namespace OpenFileTransfer.Wpf.ViewModels
{
    internal class TitleViewModel : BindableBase
    {
        public event Action OnMinimize = () => { };
        public event Action OnMaxRestore = () => { };
        public event Action OnClose = () => { };
        public event Action<MenuType> OnMenuItemClicked = (_) => { };

        public DelegateCommand MinimizeCommand { get; }
        public DelegateCommand MaxRestoreCommand { get; }
        public DelegateCommand CloseCommand { get; }

        public MenuType CurrentMenuType
        {
            get => _currentMenuType;
            set
            {
                SetProperty(ref _currentMenuType, value);
                OnMenuItemClicked.Invoke(value);
            }
        }

        public Geometry? MaxRestoreGeometry { get; set; }
        public string? MaxRestoreTooltip { get; set; }

        public bool MaxWindow
        {
            set
            {
                MaxRestoreGeometry = value ? _restoreGeometry : _maxGeometry;
                MaxRestoreTooltip = value ? "向下还原" : "最大化";
                RaisePropertyChanged(nameof(MaxRestoreGeometry));
                RaisePropertyChanged(nameof(MaxRestoreTooltip));
            }
        }

        private MenuType _currentMenuType;

        private readonly Geometry _maxGeometry;
        private readonly Geometry _restoreGeometry;

        public TitleViewModel()
        {
            MinimizeCommand = new DelegateCommand(() => { OnMinimize.Invoke(); });
            MaxRestoreCommand = new DelegateCommand(() => { OnMaxRestore.Invoke(); });
            CloseCommand = new DelegateCommand(() => { OnClose.Invoke(); });

            _maxGeometry = ResourceHelper.GetResource<Geometry>("WindowMaxGeometry");
            _restoreGeometry = ResourceHelper.GetResource<Geometry>("WindowRestoreGeometry");

            //初始化菜单
            CurrentMenuType = MenuType.Main;

            //默认正常窗口
            MaxWindow = false;
        }
    }
}
